package com.ayoka.parenting.ui.coach.applycoach;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.ApplyCoachConditionBean;
import com.ayoka.parenting.bean.PCASBean;
import com.ayoka.parenting.bean.SpinnerOptionBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.GsonUtil;
import com.jincp.common.utils.UIUtils;
import com.wheelpicker.base.WheelType;
import com.wheelpicker.picker.AddressPickerDialog;
import com.wheelpicker.picker.DatePickerDialog;
import com.wheelpicker.picker.addresspicker.AddressBean;
import com.wheelpicker.picker.addresspicker.AddressWheel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: ApplyCoachPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class ApplyCoachPresenter extends BasePresenterImpl<ApplyCoachContract.View> implements ApplyCoachContract.Presenter {

    private ApplyCoachModel model;

    private List<PCASBean> PCASBeanList;

    public ApplyCoachPresenter() {
        model = new ApplyCoachModel();
        try {
            Gson gson = GsonUtil.getInstance().getGson();
            PCASBeanList = gson.fromJson(new BufferedReader(new InputStreamReader
                    (UIUtils.getResources().getAssets().open("province.json"))), new TypeToken<List<PCASBean>>() {
            }.getType());
//            List<PCASBean> newPCASBean = digui(PCASBeanList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<SpinnerOptionBean> getApplyTypeOptionData() {
        return model.getApplyTypeOptionData();
    }

    @Override
    public List<SpinnerOptionBean> getDegreeOptionData() {
        return model.getDegreeOptionData();
    }

    @Override
    public List<SpinnerOptionBean> getParentOptionData() {
        return model.getParentOptionData();
    }

    @Override
    public List<SpinnerOptionBean> getChildrenOptionData() {
        return model.getChildrenOptionData();
    }

    @Override
    public List<SpinnerOptionBean> getCompleteOptionData() {
        return model.getCompleteOptionData();
    }

    @Override
    public void showAddressSelectorDialog(FragmentManager fragmentManager) {
        AddressPickerDialog dialog = new AddressPickerDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setType(WheelType.AddressType.PROVINCE_CITY)
                .setAddressProvider(new AddressPickerDialog.AddressProvider() {

                    @Override
                    public List<? extends AddressBean> getAddressListData(Object parentBean, String parentCode, int type) {
//                    DBManager dbManager = DBManager.getInstance();
//                    List<AddressBean> result = null;
//                    if (type == AddressPickerDialog.INDEX_TAB_PROVINCE){
//                        result = dbManager.queryProvince();
//                    } else if (type == AddressPickerDialog.INDEX_TAB_CITY){
//                        result = dbManager.queryCity(parentCode);
//                    } else if (type == AddressPickerDialog.INDEX_TAB_COUNTY){
//                        result = dbManager.queryDistrict(parentCode);
//                    }
//                    return result;
                        List<? extends AddressBean> result = null;
                        if (type == AddressWheel.INDEX_TAB_PROVINCE) {
                            result = queryProvince();
                        } else if (type == AddressWheel.INDEX_TAB_CITY) {
                            result = queryCity(parentBean, parentCode);
                        }
                        return result;
                    }
                })
                .build();
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached())return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                dialogFragment.dismissAllowingStateLoss();
                String[] values = (String[]) result;
                if (!isAttached() || result == null){
                    return;
                }
                mView.get().onAddressSelected(values[0], values[1], values[2], values[3]);
            }
        });
        dialog.show(fragmentManager, "AddressPickerDialog");
    }

    @Override
    public void showSelectorDateDialog(FragmentManager fragmentManager) {
        final DatePickerDialog dialog = new DatePickerDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setMaxMilliseconds(System.currentTimeMillis())
                .setType(WheelType.DateType.YEAR_MONTH_DAY)
                .build();
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached())return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                dialogFragment.dismissAllowingStateLoss();
                if (!isAttached() || result == null){
                    return;
                }
                mView.get().onSelectorDateCallback((long)result);
            }
        });
        dialog.show(fragmentManager, "DatePickerDialog");
    }

    @Override
    public void submitApplyCoach(String name, int applyType, String birthDate, int degree, String province, String city, String work, String contractMobile, String email, String contractAddress, int parent, int children, String wechatNumber, int complete, String experience, String extra) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.submitApplyCoach(this, name, applyType, birthDate, degree, province, city, work, contractMobile, email, contractAddress, parent, children, wechatNumber, complete, experience, extra, new SimpleRequestCallback<ApplyCoachConditionBean>() {
            @Override
            public void onSuccess(ApplyCoachConditionBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onSubmitApplyCoachCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onSubmitApplyCoachCallback(false);
            }
        });
    }

    private List<? extends AddressBean> queryProvince() {
        if (PCASBeanList == null) return new ArrayList<>();
        return PCASBeanList;
    }

    private List<? extends AddressBean> queryCity(Object parentBean, String code) {
        if (parentBean == null) return new ArrayList<>();
        if (parentBean instanceof PCASBean){
            return ((PCASBean)parentBean).children;
        }
        return new ArrayList<>();
    }
}
